package com.ruoyi.cderp.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.cderp.domain.CustomerSettleFeeSheetDetail;
import com.ruoyi.cderp.mapper.CustomerSettleFeeSheetMapper;
import com.ruoyi.cderp.domain.CustomerSettleFeeSheet;
import com.ruoyi.cderp.service.ICustomerSettleFeeSheetService;

import static com.ruoyi.common.utils.SecurityUtils.getLoginUser;

/**
 * 客户费用单Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-11
 */
@Service
public class CustomerSettleFeeSheetServiceImpl implements ICustomerSettleFeeSheetService
{
    @Autowired
    private CustomerSettleFeeSheetMapper customerSettleFeeSheetMapper;

    /**
     * 查询客户费用单
     *
     * @param id 客户费用单主键
     * @return 客户费用单
     */
    @Override
    public CustomerSettleFeeSheet selectCustomerSettleFeeSheetById(String id)
    {
        CustomerSettleFeeSheet customerSettleFeeSheet = customerSettleFeeSheetMapper.selectCustomerSettleFeeSheetById(id);
        List<CustomerSettleFeeSheetDetail>  list = customerSettleFeeSheetMapper.selectFeeSheetDetailIn(id);
//        if(customerSettleFeeSheet.getSheetType()==1){
//            list = customerSettleFeeSheetMapper.selectFeeSheetDetailIn(id);
//        }else {
//            list = customerSettleFeeSheetMapper.selectFeeSheetDetailOut(id);
//        }
        customerSettleFeeSheet.setCustomerSettleFeeSheetDetailList(list);
        return customerSettleFeeSheet;
    }

    /**
     * 查询客户费用单列表
     *
     * @param customerSettleFeeSheet 客户费用单
     * @return 客户费用单
     */
    @Override
    public List<CustomerSettleFeeSheet> selectCustomerSettleFeeSheetList(CustomerSettleFeeSheet customerSettleFeeSheet)
    {
        return customerSettleFeeSheetMapper.selectCustomerSettleFeeSheetList(customerSettleFeeSheet);
    }

    /**
     * 新增客户费用单
     *
     * @param customerSettleFeeSheet 客户费用单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertCustomerSettleFeeSheet(CustomerSettleFeeSheet customerSettleFeeSheet)
    {
        customerSettleFeeSheet.setCreateTime(DateUtils.getNowDate());
        // 获取当前登录用户对象
        LoginUser loginUser = SecurityUtils.getLoginUser();
        //创建人编号
        customerSettleFeeSheet.setCreateById(loginUser.getUser().getUserId().toString());
        //创建人名称
        customerSettleFeeSheet.setCreateBy(loginUser.getUser().getUserName());

        int rows = customerSettleFeeSheetMapper.insertCustomerSettleFeeSheet(customerSettleFeeSheet);
        insertCustomerSettleFeeSheetDetail(customerSettleFeeSheet);
        return rows;
    }

    /**
     * 修改客户费用单
     *
     * @param customerSettleFeeSheet 客户费用单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateCustomerSettleFeeSheet(CustomerSettleFeeSheet customerSettleFeeSheet)
    {
        customerSettleFeeSheet.setUpdateTime(DateUtils.getNowDate());
        // 获取当前登录用户对象
        LoginUser loginUser = SecurityUtils.getLoginUser();
        //修改人编号
        customerSettleFeeSheet.setUpdateById(loginUser.getUser().getUserId().toString());
        //修改人名称
        customerSettleFeeSheet.setUpdateBy(loginUser.getUser().getUserName());
        customerSettleFeeSheetMapper.deleteCustomerSettleFeeSheetDetailBySheetId(customerSettleFeeSheet.getId());
        insertCustomerSettleFeeSheetDetail(customerSettleFeeSheet);
        return customerSettleFeeSheetMapper.updateCustomerSettleFeeSheet(customerSettleFeeSheet);
    }

    /**
     * 批量删除客户费用单
     *
     * @param ids 需要删除的客户费用单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCustomerSettleFeeSheetByIds(String[] ids)
    {
        customerSettleFeeSheetMapper.deleteCustomerSettleFeeSheetDetailBySheetIds(ids);
        return customerSettleFeeSheetMapper.deleteCustomerSettleFeeSheetByIds(ids);
    }

    /**
     * 删除客户费用单信息
     *
     * @param id 客户费用单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCustomerSettleFeeSheetById(String id)
    {
        customerSettleFeeSheetMapper.deleteCustomerSettleFeeSheetDetailBySheetId(id);
        return customerSettleFeeSheetMapper.deleteCustomerSettleFeeSheetById(id);
    }

    /**
     * 新增客户费用单明细信息
     *
     * @param customerSettleFeeSheet 客户费用单对象
     */
    public void insertCustomerSettleFeeSheetDetail(CustomerSettleFeeSheet customerSettleFeeSheet)
    {
        List<CustomerSettleFeeSheetDetail> customerSettleFeeSheetDetailList = customerSettleFeeSheet.getCustomerSettleFeeSheetDetailList();
        String id = customerSettleFeeSheet.getId();
        if (StringUtils.isNotNull(customerSettleFeeSheetDetailList))
        {
            List<CustomerSettleFeeSheetDetail> list = new ArrayList<CustomerSettleFeeSheetDetail>();
            for (CustomerSettleFeeSheetDetail customerSettleFeeSheetDetail : customerSettleFeeSheetDetailList)
            {
                customerSettleFeeSheetDetail.setSheetId(id);
                list.add(customerSettleFeeSheetDetail);
            }
            if (list.size() > 0)
            {
                customerSettleFeeSheetMapper.batchCustomerSettleFeeSheetDetail(list);
            }
        }
    }
}
